home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / getopt.c < prev    next >
Text File  |  1990-07-08  |  13KB  |  404 lines

  1. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  2.    but it behaves differently for the user, since it allows the user
  3.    to intersperse the options with the other arguments.
  4.  
  5.    As `getopt' works, it permutes the elements of `argv' so that,
  6.    when it is done, all the options precede everything else.  Thus
  7.    all application programs are extended to handle flexible argument order.
  8.  
  9.    Setting the environment variable _POSIX_OPTION_ORDER disables permutation.
  10.    Then the behavior is completely standard.
  11.  
  12.    GNU application programs can use a third alternative mode in which
  13.    they can distinguish the relative order of options and other arguments.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <string.h>
  19. #include <malloc.h>
  20.  
  21. #define USG
  22. #ifdef USG
  23. #include <memory.h>
  24. #define bcopy(src, dst, num) memcpy((dst), (src), (num))
  25. #endif
  26.  
  27. #undef sparc
  28. #ifdef sparc
  29. #include <alloca.h>
  30. #endif
  31.  
  32. #include "getopt.h"
  33.  
  34. /* For communication from `getopt' to the caller.
  35.    When `getopt' finds an option that takes an argument,
  36.    the argument value is returned here.
  37.    Also, when `ordering' is RETURN_IN_ORDER,
  38.    each non-option ARGV-element is returned here.  */
  39.  
  40. char *optarg = 0;
  41.  
  42. /* Index in ARGV of the next element to be scanned.
  43.    This is used for communication to and from the caller
  44.    and for communication between successive calls to `getopt'.
  45.  
  46.    On entry to `getopt', zero means this is the first call; initialize.
  47.  
  48.    When `getopt' returns EOF, this is the index of the first of the
  49.    non-option elements that the caller should itself scan.
  50.  
  51.    Otherwise, `optind' communicates from one call to the next
  52.    how much of ARGV has been scanned so far.  */
  53.  
  54. int optind = 0;
  55.  
  56. /* The next char to be scanned in the option-element
  57.    in which the last option character we returned was found.
  58.    This allows us to pick up the scan where we left off.
  59.  
  60.    If this is zero, or a null string, it means resume the scan
  61.    by advancing to the next ARGV-element.  */
  62.  
  63. static char *nextchar;
  64.  
  65. /* Callers store zero here to inhibit the error message
  66.    for unrecognized options.  */
  67.  
  68. int opterr = 1;
  69.  
  70. /* Describe how to deal with options that follow non-option ARGV-elements.
  71.  
  72.    UNSPECIFIED means the caller did not specify anything;
  73.    the default is then REQUIRE_ORDER if the environment variable
  74.    _OPTIONS_FIRST is defined, PERMUTE otherwise.
  75.  
  76.    REQUIRE_ORDER means don't recognize them as options.
  77.    Stop option processing when the first non-option is seen.
  78.    This is what Unix does.
  79.  
  80.    PERMUTE is the default.  We permute the contents of `argv' as we scan,
  81.    so that eventually all the options are at the end.  This allows options
  82.    to be given in any order, even with programs that were not written to
  83.    expect this.
  84.  
  85.    RETURN_IN_ORDER is an option available to programs that were written
  86.    to expect options and other ARGV-elements in any order and that care about
  87.    the ordering of the two.  We describe each non-option ARGV-element
  88.    as if it were the argument of an option with character code zero.
  89.    Using `-' as the first character of the list of option characters
  90.    requests this mode of operation.
  91.  
  92.    The special argument `--' forces an end of option-scanning regardless
  93.    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  94.    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  95.  
  96. static enum { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER } ordering;
  97.  
  98. /* Handle permutation of arguments.  */
  99.  
  100. /* Describe the part of ARGV that contains non-options that have
  101.    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  102.    `last_nonopt' is the index after the last of them.  */
  103.  
  104. static int first_nonopt;
  105. static int last_nonopt;
  106.  
  107. /* Exchange two adjacent subsequences of ARGV.
  108.    One subsequence is elements [first_nonopt,last_nonopt)
  109.     which contains all the non-options that have been skipped so far.
  110.    The other is elements [last_nonopt,optind), which contains all
  111.     the options processed since those non-options were skipped.
  112.  
  113.    `first_nonopt' and `last_nonopt' are relocated so that they describe
  114.     the new indices of the non-options in ARGV after they are moved.  */
  115.  
  116. static void
  117. exchange (argv)
  118.      char **argv;
  119. {
  120.   int nonopts_size
  121.     = (last_nonopt - first_nonopt) * sizeof (char *);
  122.   char **temp = (char **) alloca (nonopts_size);
  123.  
  124.   /* Interchange the two blocks of data in argv.  */
  125.  
  126.   bcopy (&argv[first_nonopt], temp, nonopts_size);
  127.   bcopy (&argv[last_nonopt], &argv[first_nonopt],
  128.      (optind - last_nonopt) * sizeof (char *));
  129.   bcopy (temp, &argv[first_nonopt + optind - last_nonopt],
  130.      nonopts_size);
  131.  
  132.   /* Update records for the slots the non-options now occupy.  */
  133.  
  134.   first_nonopt += (optind - last_nonopt);
  135.   last_nonopt = optind;
  136. }
  137.  
  138. /* Scan elements of ARGV (whose length is ARGC) for option characters
  139.    given in OPTSTRING.
  140.  
  141.    If an element of ARGV starts with '-', and is not exactly "-" or "--",
  142.    then it is an option element.  The characters of this element
  143.    (aside from the initial '-') are option characters.  If `getopt'
  144.    is called repeatedly, it returns successively each of theoption characters
  145.    from each of the option elements.
  146.  
  147.    If `getopt' finds another option character, it returns that character,
  148.    updating `optind' and `nextchar' so that the next call to `getopt' can
  149.    resume the scan with the following option character or ARGV-element.
  150.  
  151.    If there are no more option characters, `getopt' returns `EOF'.
  152.    Then `optind' is the index in ARGV of the first ARGV-element
  153.    that is not an option.  (The ARGV-elements have been permuted
  154.    so that those that are not options now come last.)
  155.  
  156.    OPTSTRING is a string containing the legitimate option characters.
  157.    A colon in OPTSTRING means that the previous character is an option
  158.    that wants an argument.  The argument is taken from the rest of the
  159.    current ARGV-element, or from the following ARGV-element,
  160.    and returned in `optarg'.
  161.  
  162.    If an option character is seen that is not listed in OPTSTRING,
  163.    return '?' after printing an error message.  If you set `opterr' to
  164.    zero, the error message is suppressed but we still return '?'.
  165.  
  166.    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  167.    so the following text in the same ARGV-element, or the text of the following
  168.    ARGV-element, is returned in `optarg.  Two colons mean an option that
  169.    wants an optional arg; if there is text in the current ARGV-element,
  170.    it is returned in `optarg'.
  171.  
  172.    If OPTSTRING starts with `-', it requests a different method of handling the
  173.    non-option ARGV-elements.  See the comments about RETURN_IN_ORDER, above.  */
  174.  
  175. int
  176. getopt (argc, argv, optstring)
  177.      int argc;
  178.      char **argv;
  179.      char *optstring;
  180. {
  181.   /* Initialize the internal data when the first call is made.
  182.      Start processing options with ARGV-element 1 (since ARGV-element 0
  183.      is the program name); the sequence of previously skipped
  184.      non-option ARGV-elements is empty.  */
  185.  
  186.   if (optind == 0)
  187.     {
  188.       first_nonopt = last_nonopt = optind = 1;
  189.  
  190.       nextchar = 0;
  191.  
  192.       /* Determine how to handle the ordering of options and nonoptions.  */
  193.  
  194.       if (optstring[0] == '-')
  195.     ordering = RETURN_IN_ORDER;
  196.       else if (getenv ("_POSIX_OPTION_ORDER") != 0)
  197.     ordering = REQUIRE_ORDER;
  198.       else
  199.     ordering = PERMUTE;
  200.     }
  201.  
  202.   if (nextchar == 0 || *nextchar == 0)
  203.     {
  204.       if (ordering == PERMUTE)
  205.     {
  206.       /* If we have just processed some options following some non-options,
  207.          exchange them so that the options come first.  */
  208.  
  209.       if (first_nonopt != last_nonopt && last_nonopt != optind)
  210.         exchange (argv);
  211.       else if (last_nonopt != optind)
  212.         first_nonopt = optind;
  213.  
  214.       /* Now skip any additional non-options
  215.          and extend the range of non-options previously skipped.  */
  216.  
  217.       while (optind < argc
  218.          && (argv[optind][0] != '-'
  219.              || argv[optind][1] == 0))